package org.example.lanchain.mapper;

import org.apache.ibatis.annotations.*;
import org.example.lanchain.bean.AiAssistant;

import java.util.List;

@Mapper
public interface AiAssistantMapper {

    @Insert("INSERT INTO ai_assistant_config(" +
            "user_id, name, avatar, welcome_message, domains, response_speed, " +
            "knowledge_base_ids, work_start_time, work_end_time, auto_reply, offline_message, " +
            "system_prompt, model_config)" +
            "VALUES(" +
            "#{userId}, #{name}, #{avatar}, #{welcomeMessage}, #{domains, typeHandler=org.example.lanchain.mapper.handler.ListStringTypeHandler}, #{responseSpeed}, " +
            "#{knowledgeBaseIds}, #{workStartTime}, #{workEndTime}, #{autoReply}, #{offlineMessage}, " +
            "#{systemPrompt}, #{modelConfig})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertAiAssistant(AiAssistant aiAssistant);

    @Select("SELECT id, user_id, name, avatar, welcome_message, domains, response_speed, " +
            "knowledge_base_ids, work_start_time, work_end_time, auto_reply, offline_message, " +
            "system_prompt, model_config, create_time, update_time " +
            "FROM ai_assistant_config WHERE id = #{id}")
    @Results({
        @Result(property = "userId", column = "user_id"),
        @Result(property = "welcomeMessage", column = "welcome_message"),
        @Result(property = "domains", column = "domains", typeHandler = org.example.lanchain.mapper.handler.ListStringTypeHandler.class),
        @Result(property = "responseSpeed", column = "response_speed"),
        @Result(property = "knowledgeBaseIds", column = "knowledge_base_ids"),
        @Result(property = "workStartTime", column = "work_start_time"),
        @Result(property = "workEndTime", column = "work_end_time"),
        @Result(property = "autoReply", column = "auto_reply"),
        @Result(property = "offlineMessage", column = "offline_message"),
        @Result(property = "systemPrompt", column = "system_prompt"),
        @Result(property = "modelConfig", column = "model_config"),
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time")
    })
    AiAssistant getAiAssistantById(Long id);

    @Select({
            "<script>",
            "SELECT id, user_id, name, avatar, welcome_message, domains, response_speed, ",
            "knowledge_base_ids, work_start_time, work_end_time, auto_reply, offline_message, ",
            "system_prompt, model_config, create_time, update_time ",
            "FROM ai_assistant_config",
            "<where>",
            "<if test='userId != null'> user_id = #{userId} </if>",
            "</where>",
            "</script>"
    })
    @Results({
        @Result(property = "userId", column = "user_id"),
        @Result(property = "welcomeMessage", column = "welcome_message"),
        @Result(property = "domains", column = "domains", typeHandler = org.example.lanchain.mapper.handler.ListStringTypeHandler.class),
        @Result(property = "responseSpeed", column = "response_speed"),
        @Result(property = "knowledgeBaseIds", column = "knowledge_base_ids"),
        @Result(property = "workStartTime", column = "work_start_time"),
        @Result(property = "workEndTime", column = "work_end_time"),
        @Result(property = "autoReply", column = "auto_reply"),
        @Result(property = "offlineMessage", column = "offline_message"),
        @Result(property = "systemPrompt", column = "system_prompt"),
        @Result(property = "modelConfig", column = "model_config"),
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time")
    })
    List<AiAssistant> getAllAiAssistants(@Param("userId") Long userId);

    @Update({
            "<script>",
            "UPDATE ai_assistant_config SET ",
            "name = #{name}, ",
            "avatar = #{avatar}, ",
            "welcome_message = #{welcomeMessage}, ",
            "domains = #{domains, typeHandler=org.example.lanchain.mapper.handler.ListStringTypeHandler}, ",
            "response_speed = #{responseSpeed}, ",
            "knowledge_base_ids = #{knowledgeBaseIds}, ",
            "work_start_time = #{workStartTime}, ",
            "work_end_time = #{workEndTime}, ",
            "auto_reply = #{autoReply}, ",
            "offline_message = #{offlineMessage}, ",
            "system_prompt = #{systemPrompt}, ",
            "model_config = #{modelConfig}, ",
            "update_time = NOW() ",
            "WHERE id = #{id}",
            "</script>"
    })
    void updateAiAssistant(AiAssistant aiAssistant);

    @Delete("DELETE FROM ai_assistant_config WHERE id = #{id}")
    void deleteAiAssistant(Long id);
} 